using SS;
using System;
using System.Data;
using System.Data.SqlClient;
namespace SM.core
{
	internal class InOutBill
	{
		private Func objFunc = new Func();
		private string strConnection = "";
		public InOutBill()
		{
			Program.strConnectionString = this.objFunc.strConnectionString;
		}
		public int action(string ID, DateTime CreateDate, double TotalMoney, string TypeInput, string Note, string ObjectID, string ObjectName, string userName, string fullName, DateTime ExecuteDate)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionOnTabInOutBill", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("CreateDate", SqlDbType.DateTime);
				sqlCommand.Parameters.Add("TotalMoney", SqlDbType.Float);
				sqlCommand.Parameters.Add("TypeInput", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("Note", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("ObjectID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("ObjectName", SqlDbType.NVarChar, 500);
				sqlCommand.Parameters.Add("userName", SqlDbType.VarChar, 50);
				sqlCommand.Parameters.Add("fullName", SqlDbType.NVarChar, 250);
				sqlCommand.Parameters.Add("ExecuteDate", SqlDbType.DateTime);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.Parameters["CreateDate"].Value = CreateDate;
				sqlCommand.Parameters["TotalMoney"].Value = TotalMoney;
				sqlCommand.Parameters["TypeInput"].Value = TypeInput;
				sqlCommand.Parameters["Note"].Value = Note;
				sqlCommand.Parameters["ObjectID"].Value = ObjectID;
				sqlCommand.Parameters["ObjectName"].Value = ObjectName;
				sqlCommand.Parameters["userName"].Value = userName;
				sqlCommand.Parameters["fullName"].Value = fullName;
				sqlCommand.Parameters["ExecuteDate"].Value = ExecuteDate;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public string getOutBillID(string type)
		{
			string text = "PC-";
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT TOP 1 AutoItem FROM [TabInOutBill] WHERE [TypeInput] IN ('" + type + "') ORDER BY [AutoItem] DESC";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				if (sqlDataReader.HasRows)
				{
					while (sqlDataReader.Read())
					{
						text = (int.Parse(sqlDataReader["AutoItem"].ToString()) + 1).ToString();
					}
				}
				else
				{
					text = "1";
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
				if (type == "IN")
				{
					text = "PT-" + text;
				}
				else
				{
					text = "PC-" + text;
				}
			}
			catch
			{
				if (type == "IN")
				{
					text = "PT-";
				}
				else
				{
					text = "PC-";
				}
				result = text;
				return result;
			}
			result = text;
			return result;
		}
		public int delItem(string ID)
		{
			int result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("ActionDeleteInOutBill", sqlConnection);
				sqlCommand.CommandType = CommandType.StoredProcedure;
				sqlCommand.Parameters.Add("ID", SqlDbType.VarChar, 50);
				sqlCommand.Parameters["ID"].Value = ID;
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
				result = 0;
			}
			catch
			{
				result = 1;
			}
			return result;
		}
		public void setPrinted(string ID)
		{
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("UPDATE [TabInOutBill] SET [Printed] = 1 WHERE [ID] = '" + ID + "'", sqlConnection);
				sqlCommand.ExecuteNonQuery();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
		}
		public bool getPrinted(string ID)
		{
			bool result = false;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = new SqlCommand("SELECT [Printed] FROM [TabInOutBill] WHERE [ID] = '" + ID + "'", sqlConnection);
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					result = (sqlDataReader["Printed"].ToString() == "True");
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
			}
			return result;
		}
		public string getuserNameBillID(string ID)
		{
			string text = "-";
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT [userName] FROM [TabInOutBill] WHERE [ID] = '" + ID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					text = sqlDataReader["userName"].ToString();
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
				result = "-";
				return result;
			}
			result = text;
			return result;
		}
		public string getBillIDWithObject(string objectID)
		{
			string text = "";
			string result;
			try
			{
				SqlConnection sqlConnection = new SqlConnection(Program.strConnectionString);
				sqlConnection.Open();
				SqlCommand sqlCommand = sqlConnection.CreateCommand();
				sqlCommand.CommandText = "SELECT [ID] FROM [TabInOutBill] WHERE [ObjectID] = '" + objectID + "'";
				SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
				while (sqlDataReader.Read())
				{
					text = sqlDataReader["ID"].ToString();
				}
				sqlDataReader.Close();
				sqlConnection.Close();
				sqlConnection.Dispose();
			}
			catch
			{
				result = "-";
				return result;
			}
			result = text;
			return result;
		}
	}
}
